TEX_INPUTS = $(wildcard *.tex)

GNU_INPUTS = $(wildcard *.gnu)

RESULT_INPUTS = $(wildcard results/*.dat)

TOOLS_FOLDER = ../common/tools

BIBLIOGRAPHY_PATH = ../common/bibliographies

# What to decide when to rerun on.
RERUN = 'Rerun to get ((cross-references right)|(citations correct))'

# Make sure we know how to remove the garbage.
RM = rm -f
GARBAGE = *.ps *.dvi *.pdf *.aux *.toc *.idx *.ind *.ilg *.log *.out \
          *.lof *.lot *.loa *.bbl *.blg *~



# Default options that can be overridden in the individual makefiles.
GRAPHICS_FOLDER ?= graphics
DOCUMENT ?= default
BIBLIOGRAPHY ?= ${BIBLIOGRAPHY_PATH}/bibliography
SIZEBIB ?= $(TOOLS_FOLDER)/sizebib $(DOCUMENT).bbl

#########################################################
# Next we start by declairing the rules. (Makefile.rules)
#
# Phony targets to simplify life a bit. Note that all
# will be default.
.PHONY: all graphics force dvi ps pdf ps_2on1 html camera clean cleaner warnings

all: pdf

dvi: $(DOCUMENT).dvi

ps: $(DOCUMENT).ps

pdf: $(DOCUMENT).pdf

ps_2on1: $(DOCUMENT)_2on1.ps

# Phony target to make everything in the graphics folder.
graphics:
	$(MAKE) -C $(GRAPHICS_FOLDER) ps

# A quick and dirty fix to avoid having the file depend on everything in
# the graphics folder and still be able to force a rebuild of the document
# on changes in that folder.
force: graphics
	$(RM) $(GARBAGE)
	$(MAKE) ps

# Results will always be replotted.
$(GNU_INPUTS:.gnu=.cheese): %.cheese: %.gnu
	gnuplot $<

# Make sure LaTex looks in the proper directory and that all the images
# are in a well suited format. Then go ahead and make the document.
$(DOCUMENT).aux: $(TEX_INPUTS) $(GNU_INPUTS:.gnu=.cheese) \
	         $(RESULT_INPUTS = $(wildcard results/*.dat)
	TEXINPUTS="$(GRAPHICS_FOLDER):"; export TEXINPUTS
	$(MAKE) graphics
	@echo "Running latex..."
	latex -interaction=nonstopmode $(DOCUMENT)

# Create the bilbiography entries from the results of the first LaTex run
# together with the BibTex input.
$(DOCUMENT).bbl: $(BIBLIOGRAPHY) $(DOCUMENT).aux
	@echo "Creating bibliography..."
	$(TOOLS_FOLDER)/split_bib.sh  $(BIBLIOGRAPHY) \
                                      ${BIBLIOGRAPHY_PATH} && \
	bibtex -terse $(DOCUMENT) && \
        $(SIZEBIB)	&& \
	$(TOOLS_FOLDER)/supress $(DOCUMENT).bbl $(SUPRESS_BIBS)


# The dvi file depends on the bibliography file and we need to make
# sure that LaTex is rerun until it stabilizes.
$(DOCUMENT).dvi: $(DOCUMENT).bbl
	latex -interaction=nonstopmode $(DOCUMENT)
	latex_count=5 ; \
	while egrep -s $(RERUN) $(DOCUMENT).log && [ $$latex_count -gt 0 ] ;\
	    do \
	      echo "Rerunning latex...." ;\
	      latex -interaction=nonstopmode $(DOCUMENT);\
	      latex_count=`expr $$latex_count - 1` ;\
	    done

# Create a PostScript file from the dvi file and store it in a file, no
# sending to any printer!
$(DOCUMENT).ps: $(DOCUMENT).dvi
	dvips -Ppdf -G0 -ta4 -o $@ $^

# Use ps2pdf to do the transition from ps to pdf.
# This is the old PDF target, which is now deprecated. It will be
# removed in 2013.
#$(DOCUMENT).pdf: $(DOCUMENT).ps
#	ps2pdf -dPDFsettings=/prepress -dMaxSubsetPct=100 \
#	-dSubsetFonts=true -dEmbedAllFonts=true $^ $@

# Use ps2pdf to do the transition from ps to pdf.
$(DOCUMENT).pdf: $(DOCUMENT).ps
	ps2pdf -dPDFSETTINGS=/prepress -dEmbedAllFonts=true $^ $@

# Convenience function for getting 2 pages on one A4 in the PostScript.
$(DOCUMENT)_2on1.ps: $(DOCUMENT).ps
	psnup -2 $(DOCUMENT).ps >$(DOCUMENT)_2on1.ps

# Hack to get all fonts embedded and subset. Manually inspect and verify
# that there is no formatting errors. 
# This target is deprecated, since PDFs are now supposed to be generated
# correctly. Target will be removed in 2013.
camera: $(DOCUMENT).pdf
	pdftops $(DOCUMENT).pdf; \
	ps2pdf -dPDFSETTINGS=/prepress $(DOCUMENT).ps 

html: $(TEX_INPUTS) $(EPS_OUTPUTS) $(EPS_INPUTS)
	latex2html -split 0 -nonavigation -no_images -dir html -mkdir -notransparent \
       -white $(DOCUMENT).tex

########################################################
# Tell make how to clean up the mess that LaTeX creates.
#
# Clean up created files.
clean:
	$(MAKE) -C $(GRAPHICS_FOLDER) clean
	$(RM) $(GARBAGE)

# Clean up backup files as well.
cleaner: clean
	$(RM) *~

warnings:
	$(MAKE) ps 2>&1 | grep "Warning"